package org.hb0712.sya.tools.database.common;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;

public class RSAUtils {
    // 加密算法名称
    public static final String RSA_ALGORITHM = "RSA";
    // 字符编码
    public static final String CHARSET = "UTF-8";
    
    /**
     * 从Base64字符串加载公钥
     * @param publicKeyStr Base64编码的公钥字符串
     * @return 公钥对象
     */
    public static PublicKey getPublicKeyFromString(String publicKeyStr) throws Exception {
        byte[] decoded = Base64.decodeBase64(publicKeyStr);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decoded);
        KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
        return keyFactory.generatePublic(keySpec);
    }
    
    /**
     * 从Base64字符串加载私钥
     * @param privateKeyStr Base64编码的私钥字符串
     * @return 私钥对象
     */
    public static PrivateKey getPrivateKeyFromString(String privateKeyStr) throws Exception {
        byte[] decoded = Base64.decodeBase64(privateKeyStr);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decoded);
        KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
        return keyFactory.generatePrivate(keySpec);
    }
    
    /**
     * 公钥加密
     * @param data 待加密数据
     * @param publicKey 公钥
     * @return Base64编码的加密数据
     */
    public static String encrypt(String data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes(CHARSET));
        return Base64.encodeBase64String(encryptedBytes);
    }
    
    /**
     * 公钥加密（使用Base64编码的公钥字符串）
     * @param data 待加密数据
     * @param publicKeyStr Base64编码的公钥字符串
     * @return Base64编码的加密数据
     */
    public static String encrypt(String data, String publicKeyStr) throws Exception {
        PublicKey publicKey = getPublicKeyFromString(publicKeyStr);
        return encrypt(data, publicKey);
    }
    
    /**
     * 私钥解密
     * @param encryptedData Base64编码的加密数据
     * @param privateKey 私钥
     * @return 解密后的原始数据
     */
    public static String decrypt(String encryptedData, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decodedData = Base64.decodeBase64(encryptedData);
        byte[] decryptedBytes = cipher.doFinal(decodedData);
        return new String(decryptedBytes, CHARSET);
    }
    
    /**
     * 私钥解密（使用Base64编码的私钥字符串）
     * @param encryptedData Base64编码的加密数据
     * @param privateKeyStr Base64编码的私钥字符串
     * @return 解密后的原始数据
     */
    public static String decrypt(String encryptedData, String privateKeyStr) throws Exception {
        PrivateKey privateKey = getPrivateKeyFromString(privateKeyStr);
        return decrypt(encryptedData, privateKey);
    }
}
